package com.bangdream.michelia.view.fragment.main.curriculum;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.andview.refreshview.XRefreshView;
import com.bangdream.michelia.R;
import com.bangdream.michelia.contract.CoursesContract;
import com.bangdream.michelia.entity.RxEventBean;
import com.bangdream.michelia.entity.VideoRxBean;
import com.bangdream.michelia.entity.courses.CourseInfoBean;
import com.bangdream.michelia.entity.courses.VideoMsgBean;
import com.bangdream.michelia.presenter.CoursesPresenter;
import com.bangdream.michelia.utils.L;
import com.bangdream.michelia.utils.RxBus;
import com.bangdream.michelia.utils.ToastUtil;
import com.bangdream.michelia.view.adapter.CourseChapterAdapter;
import com.bangdream.michelia.view.fragment.currency.BaseFragment;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

/**
 * @date on 2018/7/23 17:51
 * @email 909732184@qq.com
 * 课程详情-章节
 */
public class CurriculumDetailsChapter extends BaseFragment<CoursesContract.ICoursesInfo, CoursesPresenter<CoursesContract.ICoursesInfo>>
        implements CoursesContract.ICoursesInfo {
    private View view;
    private XRefreshView refreshView;
    private RecyclerView recyclerView;
    private LinearLayoutManager linearLayoutManager;
    private CourseChapterAdapter adapter;
    private CourseInfoBean data;
    private TextView tvNumber;

    private int listPositon = 0;
    String isOrder="0";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.curriculum_chapter, container, false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init();
    }

    @Override
    public CoursesPresenter createPresenter() {
        return new CoursesPresenter();
    }

    private void init() {
        if (getArguments() != null) {
            data = getArguments().getParcelable("data");
        }
        isOrder = data.getIsOrder();
        refreshView = view.findViewById(R.id.refreshView);
        tvNumber = view.findViewById(R.id.tvNumber);
        refreshView.setPullLoadEnable(false);
        refreshView.setPullRefreshEnable(false);
        linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView = view.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(linearLayoutManager);
        adapter = new CourseChapterAdapter(R.layout.curriculum_chapter_adapter_item);
        recyclerView.setAdapter(adapter);
        setViewData(data);
    }


    private void setViewData(CourseInfoBean bean) {
        this.data = bean;
        if (data == null)
            return;
        L.d(data.toString());
        tvNumber.setText("(共" + (data.getChapterInfos() == null ? "0" : data.getChapterInfos().size()) + "个章节)");
        adapter.setNewData(data.getChapterInfos());

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter a, View view, int position) {
//                if(data.isBuy()){
                    initVideo(position);
//                }else {
//                    ToastUtil.showShortToast(getActivity(),"您还未购此课程");
//                }
            }
        });
//        if(data.isBuy())

        List<CourseInfoBean.ChapterInfosBean> dataList = adapter.getData();

        int firstPlayPositon=0;
        if(!dataList.isEmpty()){
            for(int i=0;i<dataList.size();i++){
                if(dataList.get(i).getStatus().equals("2")){
                    firstPlayPositon++;
                }
            }

            if(firstPlayPositon==dataList.size())
                firstPlayPositon=0;
        }

        initVideo(firstPlayPositon);

    }


    public void initVideo(int position) {
        if (adapter.getData().size() > position && adapter.getData().get(position) != null) {
            //是否顺序学习
            if(isOrder!=null && isOrder.equals("1")){
                if(position>0){
                    if(adapter.getData().get(position-1).getStatus().equals("2")){
                        play(position);
                    }else{
                        ToastUtil.showShortToast(getActivity(),"请先学习前面的内容");
                    }
                }else{
                    play(position);
                }
            }else{
                play(position);
            }
        } else {
            if(position==0){
                ToastUtil.showShortToast(getActivity(),"暂无资源");
            }else{
                adapter.setFinish();
            }
        }
    }

    public void setLearned(int position){
        if(adapter.getData().size()>position){
            adapter.getData().get(position).setStatus("2");
        }
    }

    public void play(int position){
//        TextView textView = (TextView) adapter.getViewByPosition(recyclerView,position, R.id.tvGoExamination);
//        if(textView!=null)
//            if(!adapter.getData().get(position).getStatus().equals("2")){
//                textView.setText("学习中");
//                adapter.notifyDataSetChanged();
//            }

        List<CourseInfoBean.ChapterInfosBean.SourceListBean> sourceList = adapter.getData().get(position).getSourceList();

        if (null != sourceList && sourceList.size() > 0) {
            listPositon=position;
            mPresenter.getVideoMsg(adapter.getData().get(position).getSourceList().get(0).getSourceId(), pd);
            adapter.setDefSelect(position);
        } else {
            Toast.makeText(getActivity(), "暂无资源", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void setCourseInfo(boolean isSuccess, CourseInfoBean bean) {
    }

    @Override
    public void setVideo(VideoMsgBean url) {
        RxEventBean bean = new RxEventBean();
        bean.setCode(0x01);
        bean.setPosition(listPositon);

        VideoRxBean rxBean=new VideoRxBean();
        CourseInfoBean.ChapterInfosBean chapterInfosBean = adapter.getData().get(listPositon);
        rxBean.setLearnHour(chapterInfosBean.getLearnHour());
        if(chapterInfosBean.getAllHour()!=null && chapterInfosBean.getLearnHour()>0){
            Double aDouble = Double.valueOf(chapterInfosBean.getAllHour());
            rxBean.setAllHour(aDouble);
            double v = chapterInfosBean.getLearnHour()/aDouble;
            if(v>0.98){
                rxBean.setLearnHour(0L);
            }
        }

        rxBean.setStatus(chapterInfosBean.getStatus());
        rxBean.setChapterId(chapterInfosBean.getId());
        rxBean.setId(chapterInfosBean.getChapterLearnId());
        rxBean.setUrl(url.getFilePathMp4());
        bean.setMsg(rxBean);
        RxBus.getInstance().post(bean);
    }
}
